package com.shop2cn.iapi.prodstockmgr.testcase;

import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonArray;
import com.shop2cn.iapi.prodstockmgr.parameter.AddExportTaskBean;
import com.shop2cn.iapi.prodstockmgr.service.AddExportTaskCall;
import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.business.service.Shop2cnStockCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.utils.StockExcleUtil;
import com.ymttest.utils.StockTestCaseBean;

/**
 * 创建导出任务Test FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_AddExportTask {
	private static AddExportTaskBean addexporttaskBean;
	private static AddExportTaskCall addexporttaskCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("创建导出任务Test");
	}

	@Before
	public void caseUp() {
		addexporttaskBean = new AddExportTaskBean();
		addexporttaskCall = new AddExportTaskCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(addexporttaskCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_AddExportTask_001() {
		Logger.start(true, "正确数据，成功");
		try {
			Integer mchid = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(mchid,
					"", true);
			
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			//进行一次出库和入库操作
			Shop2cnStockCallService.prodstockstockIn(mchid, productId, 10);
			addexporttaskBean.setSellerId(mchid);
			addexporttaskCall.setData(addexporttaskBean);
			addexporttaskCall.callService();
			Logger.verifyEquals("200", addexporttaskCall.getString("code"), "验证code");
			Shop2cnStockCallService.Execute();
			Thread.sleep(3000);
			JsonArray getTaskList = Shop2cnStockCallService.GetTaskList(mchid, 2);
			String fileUrl = getTaskList.get(0).getAsJsonObject().get("fileUrl").getAsString();
			StockTestCaseBean download = StockExcleUtil.Download(fileUrl).get(0);
			Logger.verifyEquals(download.getStocknum(),10,"验证导出的数据是否正确");
			Logger.verifyEquals(download.getSku(),addAgentProductBean.getCatalogList().get(0).getSku(),"验证导出的数据是否正确");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_AddExportTask_002() {
		Logger.start(true, "创建导出任务，根据商品名称查询");
		try {
			Integer mchid = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(mchid,
					"", true);
			
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			//进行一次出库和入库操作
			Shop2cnStockCallService.prodstockstockIn(mchid, productId, 10);
			addexporttaskBean.setSellerId(mchid);
			addexporttaskBean.setPrductName(addAgentProductBean.getTitle());
			addexporttaskCall.setData(addexporttaskBean);
			addexporttaskCall.callService();
			Logger.verifyEquals("200", addexporttaskCall.getString("code"), "验证code");
			Shop2cnStockCallService.Execute();
			Thread.sleep(3000);
			JsonArray getTaskList = Shop2cnStockCallService.GetTaskList(mchid, 2);
			String fileUrl = getTaskList.get(0).getAsJsonObject().get("fileUrl").getAsString();
			System.out.println(fileUrl);
			StockTestCaseBean download = StockExcleUtil.Download(fileUrl).get(0);
//			Logger.verifyEquals(download.getStocknum(),10,"验证导出的数据是否正确");
//			Logger.verifyEquals(download.getSku(),addAgentProductBean.getCatalogList().get(0).getSku(),"验证导出的数据是否正确");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
}